import { Ref } from "@vue/reactivity";
import { onMounted } from "@vue/runtime-core";

export function useModel() {
  const ctsearchModel = (searchColumn: []): object => {
    let mo: any = {};
    searchColumn.forEach((v: any) => {
      if (v?.prop) mo[v.prop] = "";
    });
    return mo;
  };
  const ctfromModel = (columns: []) => {
    let mo: any = {};
    columns.forEach((v: any) => {
      if (v.type === 'select'&& v.mode === 'multiple'){
        mo[v.prop] = [];
      }
      else{
        mo[v.prop] = '';
      }
    });
    return mo;
  };
  return {
    ctsearchModel,
    ctfromModel,
  };
}

export const useDTable = (
  emit: any,
  visible:Function,
  addFromModel: any,
  initialModel:any,
  state: Ref<string>
) => {
  const addFromEmit = (submit: any) => {
    if(state.value ==='add'){
      emit("addForm", submit);
    }
    if(state.value === 'edit')
      emit('editFrom',submit)
  };
  const add = () => {
    visible(true);
    state.value = "add";
    //initialModel 此值为原始默认的空值 用于赋默认值的， 未避免引用传递 故进行简化深拷贝
    addFromModel.value = JSON.parse(JSON.stringify(initialModel));
  };
  const searchSubmit = (model: any) => {
    emit('search',model)
  };
  const edit =  (model: any)=> {
    visible(true);
    // Object.keys(addFromModel.value).forEach((key: any) => {
    //   addFromModel.value[key] = model[key];
    // });
    addFromModel.value = model
    state.value = "edit";
  };
  const del = (model: any) => {
    emit("del", addFromModel.value);
  };
  const pageChange = (page: any) => {
    emit("pageChange", page);
  };
  return {
    addFromEmit,
    add,
    searchSubmit,
    edit,
    del,
    pageChange
  };
};

export const init = () => {};
